import Vue from 'vue'
import Vuex from 'vuex'
import { 
	loadUserInfo, saveUserInfo,clearUserInfo, 
	saveSearch, clearSearch, loadSearch,
	saveSearchGame, clearSearchGame, loadSearchGame,
	loadSearchIdle, saveSearchIdle, clearSearchIdle
} from '@/config/cache';

import { User } from '@/models/user.js';
import DES3 from '@/common/des3.js';

let user, timer;
const TOKEN = uni.getStorageSync("Token") || {};		
	
Vue.use(Vuex)
	

const store = new Vuex.Store({
	state: {
		oneLogin: false,
		indexPage: 'home',
		token: TOKEN,
		userInfo: loadUserInfo(),
        searchHistory: loadSearch(),
        searchGameHistory: loadSearchGame(),
		searchIdleHistory: loadSearchIdle(),
		curretPublisher: {},
		selectAddress: {},
		selectOrder: {},
		selectImg: {},
		timerIdent: false,//全局1s定时器，只在全局开启一个，所有需要定时执行的任务监听该值即可，无需额外开启
	},
	mutations: {
		
		//更新state数据
		setStateAttr(state, param){
			if(param instanceof Array){
				for(let item of param){
					state[item.key] = item.val;
				}
			}else{
				state[param.key] = param.val;
			}
		},
		// 设置显示的当前首页
		'SET_INDEX_PAGE'(state, page) {
			state.indexPage = page;
		},
		
		// 设置搜索记录
		'SET_SEARCH_HISTORY'(state, history) {
			state.searchHistory = history
		},
		
		// 设置搜索活动记录
		'SET_SEARCH_GAME_HISTORY'(state, history) {
			state.searchGameHistory = history
		},
		
		// 设置搜索闲本记录
		'SET_SEARCH_IDLE_HISTORY'(state, history) {
			state.searchIdleHistory = history
		},
		
		// 设置Token
		'SET_TOKEN'(state, token) {
			state.token = token
			uni.setStorageSync("Token", token);
		},
		
		// 设置用户信息
		'SET_USER_INFO'(state, userInfo) {
			state.userInfo = userInfo
		},
		
		// 防止重复条登录
		'SET_ONE_LOGIN'(state, flag) {
			state.oneLogin = flag
		},
	},
	actions: {
		/**
		 * 登录
		 */
		async appLogin({commit}, query) {
			let desKey = DES3.decrypt(query.sessionkey)
			let sessionid = DES3.decryptByDES(query.sessionid, desKey)
			user = new User()
			user.updateUserData(query)
			commit('SET_TOKEN', {
				sessionid,
				username: query.username,
				userid: query.username
			})
			commit('SET_USER_INFO', saveUserInfo(query))
		},
		
		
		
		/**
		 * 退出登录
		 */
		appLogout({commit}) {
			commit('SET_TOKEN', {})
			commit('SET_USER_INFO', clearUserInfo())
		},
		
		
		/**
		 * 拦截，防止多个接口报996（节流）
		 */
		loginLoading({commit}) {
			commit('SET_ONE_LOGIN', true)
			if(timer) {
				clearTimeout(timer)
			}
			timer = setTimeout(() => {
				commit('SET_ONE_LOGIN', false)
			}, 1000)
			
		},
		
		/**
		 * 获取用户信息
		 */
		async appGetUser({commit}) {
			user = new User()
			let info = await user.updateUserInfo();
			user.updateUserData(info)
			commit('SET_USER_INFO', saveUserInfo(info))
		},
		
		
		/**
		 * 保存搜索记录
		 * @param {*} query 
		 */
		saveSearchHistory({commit}, query) {
			commit('SET_SEARCH_HISTORY', saveSearch(query))
		},
		
		/**
		 * 清空搜索记录
		 */
		clearSearchHistory({commit}, query) {
		    commit('SET_SEARCH_HISTORY', clearSearch())
		},
		
		/**
		 * 保存搜索活动记录
		 * @param {*} query 
		 */
		saveSearchGameHistory({commit}, query) {
			commit('SET_SEARCH_GAME_HISTORY', saveSearchGame(query))
		},
		
		/**
		 * 清空搜索活动记录
		 */
		clearSearchGameHistory({commit}, query) {
		    commit('SET_SEARCH_GAME_HISTORY', clearSearchGame())
		},
		
		
		/**
		 * 保存搜索闲本记录
		 * @param {*} query 
		 */
		saveSearchIdleHistory({commit}, query) {
			commit('SET_SEARCH_IDLE_HISTORY', saveSearchIdle(query))
		},
		
		/**
		 * 清空搜索闲本记录
		 */
		clearSearchIdleHistory({commit}, query) {
		    commit('SET_SEARCH_IDLE_HISTORY', clearSearchIdle())
		}
		
	}
})


export default store
